﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbGenerator.Model.units.column;
using Unity.Errors.types;

namespace DbExecuter.DataLayer.commands
{
    public class TInsertClause
    {
        #region ctors
        public TInsertClause(TColumn[] insertColumns, TCommandParameter[] insertParameters)
        {
            SErrorManager.Errors("ValuesMismatch")
                .Assert(insertColumns.Length == insertParameters.Length
                , new TErrorInfo(GetType(), "TInsertClause")
                , "insertColumns.Length", "insertParameters.Length"
                , insertColumns.Length, insertParameters.Length);
            _InsertColumns = insertColumns;
            _InsertParameters = insertParameters;
        }
        #endregion

        #region properties
        private TCommandParameter[] _InsertParameters;
        public TCommandParameter[] InsertParameters
        {
            get { return _InsertParameters; }
        }

        private TColumn[] _InsertColumns;
        public TColumn[] InsertColumns
        {
            get { return _InsertColumns; }
        }
        #endregion

        #region overriden methods
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            //loop between columns:
            //INSERT INTO ( [Name], [Adress] )
            int intoColumnCount = _InsertColumns.Length;
            sb.Append("INSERT INTO( ");
            for (int i = 0; i < intoColumnCount; i++)
            {
                sb.AppendFormat("\n\t[{0}], "
                    , _InsertColumns[i].ColumnInfo.ColumnName);
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append(" )");

            //Now add VALUES( @p1, @p2..)
            sb.Append(" VALUES( ");
            for (int i = 0; i < intoColumnCount; i++)
            {
                sb.AppendFormat("{0}, ", _InsertParameters[i].ParameterName);
            }
            sb.Remove(sb.Length - 2, 2);
            sb.Append(" )");
            return sb.ToString();
        }
        #endregion
    }
}
